package fetcher

//抓取器
import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

var rateLimit = time.Tick(10 * time.Millisecond)

// 抓取数据
func Fetch(url string) ([]byte, error) {
	<-rateLimit
	// GET 403 模拟浏览器访问
	client := &http.Client{}
	newurl := strings.Replace(url, "http://", "https://", 1)
	req, err := http.NewRequest("GET", newurl, nil)
	if err != nil {
		fmt.Println("failed")
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36")
	cookie1 := "sid=98d976ec-384f-43c9-ae4c-810f09bac687; ec=XGL2Ats8-1620463087787-0e374998abc561688022016; FSSBBIl1UgzbN7NO=5MEGGEFWx4bLmmhTGgCi.3XBMkf9KMf48Fl9Yket.C2ADURB2U.DweinzUEJlrYpczluGXAmu3VZnHazmohlfFq; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1620463148,1620529552,1621171758; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1621257302; _exid=130L8dXb227cHqW%2BUUc%2BQrM2QeAQrlX6q1mzJhZp4N%2FE42yzv4xc9a46YFyV%2F%2FeImJqhU%2BhCc%2BdSFIiFK%2BCmSA%3D%3D; _efmdata=bVjR0R6J8cEnlECdNy5ft0uUMGnUdq%2BwEe0u7bLhzI1KYmkfvBgBPpM%2FWGR1K%2B8D5wXgGFEkK%2FKTOCI8jo6xYL89mvXkwpWrHwrWazu%2Ft1w%3D; FSSBBIl1UgzbN7NP=53tx7FbcPMD3qqqmy8vwdca1A._FK7vH2gEqlNVG_YhaqJ9eU8V8aYmnyUJKJ2b6UT8Ww7Puygd5Fp_ygMRJqoBNsq1F3lqmwGRT2NOyYQaFd0weUdhRYbzi_AsZ3wFHAwtE4EblTkoOm9M29mUgkJK6Xss4hB86R5qXYliukYd3E0P6_U8G8wlxU4TPm8i4Laalr1mWN_XUjnGpb5qqEFIns1dDPNkxb37NF6AkuhG217AEELHNl9Ts18eDc6JMYQ"
	req.Header.Add("cookie", cookie1)

	resp, err := client.Do(req)

	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// StatusCode状态码    StatusOK/200
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Wrong status code:%d", resp.StatusCode)
	}
	return ioutil.ReadAll(resp.Body)
}
